<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>
    
    <!-- 主要内容区域 -->
    <div class="login-content">
      <!-- 左侧信息区域 -->
      <div class="login-info">
        <div class="info-content">
          <div class="logo-section">
            <div class="logo-icon">
              <svg-icon icon-class="user" class="justice-icon" />
            </div>
            <h1 class="system-title">法律援助管理系统</h1>
            <p class="system-subtitle">Legal Aid Management System</p>
          </div>
          
          <div class="feature-list">
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="user" />
              </div>
              <div class="feature-text">
                <h3>专业服务</h3>
                <p>为困难群众提供专业法律援助服务</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="documentation" />
              </div>
              <div class="feature-text">
                <h3>智能管理</h3>
                <p>高效便捷的案件管理和流程控制</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="chart" />
              </div>
              <div class="feature-text">
                <h3>数据分析</h3>
                <p>全面的数据统计和分析报告</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-form-container">
        <div class="login-form-wrapper">
          <div class="form-header">
            <h2 class="welcome-text">欢迎登录</h2>
            <p class="login-desc">请输入您的账号信息进行身份验证</p>
          </div>
          
          <el-form 
            ref="loginForm" 
            :model="loginForm" 
            :rules="loginRules" 
            class="login-form"
            @submit.native.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="请输入用户名"
                size="large"
                prefix-icon="el-icon-user"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="请输入密码"
                size="large"
                prefix-icon="el-icon-lock"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
            
            <el-form-item prop="code" v-if="captchaEnabled">
              <div class="captcha-container">
                <el-input
                  v-model="loginForm.code"
                  auto-complete="off"
                  placeholder="请输入验证码"
                  size="large"
                  prefix-icon="el-icon-picture"
                  @keyup.enter.native="handleLogin"
                />
                <div class="captcha-image" @click="getCode">
                  <img :src="codeUrl" alt="验证码" />
                  <div class="captcha-refresh">
                    <svg-icon icon-class="refresh" />
                  </div>
                </div>
              </div>
            </el-form-item>
            
            <div class="form-options">
              <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
              <a href="#" class="forgot-password">忘记密码？</a>
            </div>
            
            <el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                size="large"
                class="login-button"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">立即登录</span>
                <span v-else>登录中...</span>
              </el-button>
            </el-form-item>
            
            <div class="register-link" v-if="register">
              <span>还没有账号？</span>
              <router-link to="/register" class="link-type">立即注册</router-link>
            </div>
          </el-form>
        </div>
      </div>
    </div>
    
    <!-- 底部版权信息 -->
    <div class="login-footer">
      <div class="footer-content">
        <p class="copyright">© 2024 法律援助管理系统 版权所有</p>
        <p class="slogan">让法律温暖每一个需要帮助的人</p>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: "法律援助管理系统",
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的用户名" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  
  .decoration-circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    
    &.circle-1 {
      width: 300px;
      height: 300px;
      top: -150px;
      right: -150px;
    }
    
    &.circle-2 {
      width: 200px;
      height: 200px;
      bottom: -100px;
      left: -100px;
    }
    
    &.circle-3 {
      width: 150px;
      height: 150px;
      top: 50%;
      left: 10%;
      transform: translateY(-50%);
    }
  }
}

.login-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  position: relative;
  z-index: 2;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.login-info {
  flex: 1;
  max-width: 500px;
  margin-right: 60px;
  color: white;
  
  .info-content {
    .logo-section {
      text-align: center;
      margin-bottom: 60px;
      
      .logo-icon {
        width: 80px;
        height: 80px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 20px;
        backdrop-filter: blur(10px);
        
        .justice-icon {
          font-size: 40px;
          color: white;
        }
      }
      
      .system-title {
        font-size: 32px;
        font-weight: 600;
        margin: 0 0 10px;
        letter-spacing: 2px;
      }
      
      .system-subtitle {
        font-size: 16px;
        opacity: 0.8;
        margin: 0;
        font-weight: 300;
      }
    }
    
    .feature-list {
      .feature-item {
        display: flex;
        align-items: center;
        margin-bottom: 30px;
        
        .feature-icon {
          width: 50px;
          height: 50px;
          background: rgba(255, 255, 255, 0.2);
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 20px;
          backdrop-filter: blur(10px);
          
          .svg-icon {
            font-size: 24px;
            color: white;
          }
        }
        
        .feature-text {
          h3 {
            font-size: 18px;
            font-weight: 600;
            margin: 0 0 5px;
          }
          
          p {
            font-size: 14px;
            opacity: 0.8;
            margin: 0;
            line-height: 1.5;
          }
        }
      }
    }
  }
}

.login-form-container {
  flex: 1;
  max-width: 450px;
  
  .login-form-wrapper {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border-radius: 20px;
    padding: 40px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    
    .form-header {
      text-align: center;
      margin-bottom: 30px;
      
      .welcome-text {
        font-size: 28px;
        font-weight: 600;
        color: #2c3e50;
        margin: 0 0 10px;
      }
      
      .login-desc {
        font-size: 14px;
        color: #7f8c8d;
        margin: 0;
      }
    }
    
    .login-form {
      .el-form-item {
        margin-bottom: 20px;
        
        .el-input {
          .el-input__inner {
            height: 50px;
            border-radius: 12px;
            border: 2px solid #e8f0fe;
            background: #f8fafc;
            transition: all 0.3s ease;
            
            &:focus {
              border-color: #667eea;
              background: white;
              box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            }
          }
          
          .el-input__prefix {
            left: 15px;
            
            .el-input__icon {
              font-size: 18px;
              color: #667eea;
            }
          }
        }
      }
      
      .captcha-container {
        display: flex;
        gap: 15px;
        
        .el-input {
          flex: 1;
        }
        
        .captcha-image {
          position: relative;
          width: 120px;
          height: 50px;
          border-radius: 12px;
          overflow: hidden;
          cursor: pointer;
          border: 2px solid #e8f0fe;
          
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
          
          .captcha-refresh {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.5);
            border-radius: 50%;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 0;
            transition: opacity 0.3s ease;
            
            .svg-icon {
              color: white;
              font-size: 14px;
            }
          }
          
          &:hover .captcha-refresh {
            opacity: 1;
          }
        }
      }
      
      .form-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 25px;
        
        .el-checkbox {
          .el-checkbox__label {
            color: #7f8c8d;
            font-size: 14px;
          }
        }
        
        .forgot-password {
          color: #667eea;
          text-decoration: none;
          font-size: 14px;
          
          &:hover {
            text-decoration: underline;
          }
        }
      }
      
      .login-button {
        width: 100%;
        height: 50px;
        border-radius: 12px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: none;
        font-size: 16px;
        font-weight: 600;
        letter-spacing: 1px;
        transition: all 0.3s ease;
        
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        &:active {
          transform: translateY(0);
        }
      }
      
      .register-link {
        text-align: center;
        margin-top: 20px;
        font-size: 14px;
        color: #7f8c8d;
        
        .link-type {
          color: #667eea;
          text-decoration: none;
          font-weight: 600;
          margin-left: 5px;
          
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
}

.login-footer {
  padding: 20px;
  text-align: center;
  color: white;
  position: relative;
  z-index: 2;
  
  .footer-content {
    .copyright {
      font-size: 14px;
      margin: 0 0 5px;
      opacity: 0.8;
    }
    
    .slogan {
      font-size: 12px;
      margin: 0;
      opacity: 0.6;
      font-style: italic;
    }
  }
}

// 响应式设计
@media (max-width: 1024px) {
  .login-content {
    flex-direction: column;
    gap: 40px;
  }
  
  .login-info {
    margin-right: 0;
    text-align: center;
    
    .info-content .feature-list .feature-item {
      justify-content: center;
    }
  }
}

@media (max-width: 768px) {
  .login-content {
    padding: 20px 15px;
  }
  
  .login-form-container .login-form-wrapper {
    padding: 30px 25px;
  }
  
  .login-info .info-content .logo-section .system-title {
    font-size: 24px;
  }
}
</style>
